﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using RegulatoryPlatform.DB;
using RegulatoryPlatform.Models;

namespace RegulatoryPlatform.ScheduleTask
{
    /// <summary>
    /// 电子处方明细表
    /// </summary>
    public class YL_DZCFXX : Base
    {

        public override async Task Execute(IJobExecutionContext context)
        {
            string param = DateTime.Now.ToString("yyyyMMdd");
            var json = rDData.GetDataByMethod("Address_Get_Prescription_List", new Prescription_ListRequest
            {
                KDRQ = param
            });

            var response = Serialize<PrescriptionResponse>(json);

            if (response.status == 10000 && response.msg == "success")
            {
                try
                {

                    List<interface_adviceorder> regList = new List<interface_adviceorder>();
                    if (response.content.Count > 0)
                    {
                        string[] cyhList = response.content.Select(s => s.cyh).ToArray();

                        regList = hlwDB.interface_adviceorder.AsNoTracking().Where(s => cyhList.Contains(s.prescriptionNo)).Select(s => new interface_adviceorder
                        {
                            prescriptionNo = s.prescriptionNo,
                            UpdateDate = s.UpdateDate
                        }).ToList();
                    }

                    foreach (var item in response.content)
                    {
                        foreach (var detail in item.detail)
                        {
                            TB_YL_DZCFMX entity = new TB_YL_DZCFMX();
                            entity.YLJGDM = YLJGDM;
                            entity.CYH = item.cyh;
                            entity.CFMXH = detail.cfmxh;

                            var orderEntiy = regList.FirstOrDefault(s => s.prescriptionNo == item.cyh);
                            entity.KFSJ = orderEntiy != null ? orderEntiy.CreatDate.Value : DateTime.Today;

                            entity.XMBMYB = "";
                            entity.JXDM = detail.jxdm;
                            entity.YF = detail.yf;
                            entity.YYTS = detail.yyts;
                            entity.YPSL = detail.ypsl;
                            entity.YPDW = detail.ypdw;
                            entity.SYPCDM = detail.sypcdm;
                            entity.JL = detail.jl;
                            entity.DW = detail.dw;
                            entity.CFTS = item.detail.Count;
                            entity.JYDM = detail.jydm;

                            entity.XGBZ = item.xgbz;

                            db.Set<TB_YL_DZCFMX>().Add(entity);
                        }
                    }
                    await db.BulkSaveChangesAsync();
                    await Console.Out.WriteLineAsync($"电子处方信息 数据同步完成 在{DateTime.Now}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"电子处方明细 出错了:{ex.Message}");
                    JobExecutionException e2 = new JobExecutionException(ex);
                    // 这个工作将立即重新开始  
                    e2.RefireImmediately = true;
                    throw e2;
                }
            }
            else
            {
                await Console.Out.WriteLineAsync($"未能找到 {param} 对应的 电子处方信息 ");
            }
        }

    }
    /// <summary>
    /// 电子处方明细 入参
    /// </summary>
    public class Prescription_ListRequest
    {
        /// <summary>
        /// 开单日期 YYYYMMDD
        /// </summary>
        public string KDRQ { get; set; }
    }
}
